<?php

namespace mikkle\tp_auth;
use mikkle\tp_master\Exception;
use think\facade\Config;
use think\facade\Request;
use mikkle\tp_master\Cache;
use think\facade\Route;
class WlAuthAction
{

    public function authBase(Request $request)
    {

        $all = $request::get();

        $rurl = '';
        foreach ($all as $key => $value) {
            if ($key == 'rurl') {
                $regist = TRUE;
                $rurl = $value;
            } else {
                if (strstr($rurl, '?')) {
                    $rurl = $rurl . '&' . $key . '=' . $value;
                } else {
                    $rurl = $rurl . '?' . $key . '=' . $value;
                }
            }
        }
        if ($regist == FALSE) {
            $this->errorDisplay('页面调用错误', '请传递授权页面');
        }
        $ts = time() . rand(1000, 9999);
        Cache::set($ts,array('rurl' => $rurl));

        $myurl = Route::buildUrl('wl/auth2');
        $myurl = str_replace(':80', '', $myurl);
        $myurl = str_replace(':443', '', $myurl);

        if(strpos($myurl,'sst.trip') !== false) {
            if(!strpos($myurl,'https')) {
                $myurl = str_replace('http', 'https', $myurl);
            }
        }

        $res = $this->getcodeUrl($ts, $myurl, $wxid);
        redirect($res);
    }

    public function getcodeUrl($rurl, $myurl, $wxid)
    {
        if (empty($myurl)) {
            $myurl = urlencode('http://sylyx.cn/index.php/Wx/getInfoAouth');//只要是授权域名之下的地址就可以
        } else {
            $myurl = urlencode($myurl);
        }
        $option = Config::get("wxpay.default_options_name");
        dump($option);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appId . "&redirect_uri=" . $myurl . "&response_type=code&scope=snsapi_userinfo&state=" . $rurl . "#wechat_redirect";
        return $url;
    }

}